//+------------------------------------------------------------------+
//|                                                 S&R detector.mq4 |
//|                                                        Tal Perry |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Tal Perry"
#property link      ""

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+

int SnRCount[];
double SnR[];

int SortSnR ()
   {
   double Temp;
   int RTemp,i,j;
      for (i =0; i < ArraySize(SnR) ; i++)
         {
            for (j = i+1; j <ArraySize(SnR); j++)
               {
               if ( SnR[i] >SnR[i+j] )
                  {
                     Temp =SnR[i];
                     RTemp =SnRCount[i];
                     SnR[i] = SnR[i+j];
                     SnRCount[i] = SnRCount[i+j];
                     SnR[i+j] = Temp;
                     SnRCount [i+j] =Temp;
                  }
               } 
         }
   }                  
                     
            
   
   
int start()
  {
  int i =0;
  double CurrentHigh, CurrentLow;
  int Place;
  
 int SearchTime = 1000;
 double Tolerance;
  
//----
   ObjectsDeleteAll(0); // clears the screen
   
   // Get the range of the current screen
   
   CurrentHigh = WindowPriceMax(0);
   CurrentLow = WindowPriceMin(0);
   
  
  // Sets the tolerance according to Timeframe
 Alert(Period());
  switch (Period())
  {
   case 1: Tolerance =5;
   case 5: Tolerance =8; 
   case 15: Tolerance = 13;
   case 30: Tolerance =21;
   case 60: Tolerance = 34;
   case 240: Tolerance  =55;
   case 1440: Tolerance = 89;
   case 10080: Tolerance = 144;
  }   
   
  
  
   // Begin search between range
    // Alert (ArraySize(SnR), "Size ", ArraySize(SnRCount)); 
   for (i =SearchTime ; i>=1; i--)
   
      {
      
      
            // This Part Finds Resistance        
            
               if (High[i] > High [i+1] && High[i] >High[i-1])
                  {// if a high has been found
               
                    // Compare to previous prices
                   
                   
                   
                   
                   if (ArraySize(SnR) > 0)
                   {
                     Place = ArrayBsearch(SnR,High[i]);
                   }
                   
                   else // if arraysize is 0 then open 1 cell
                      {
                      ArrayResize(SnR,ArraySize(SnR) + 1);
                      ArrayResize(SnRCount,ArraySize(SnRCount) +1);
                      SnR [ ArraySize(SnR) -1 ] = 0;
                      SnRCount [ArraySize(SnRCount) -1] = 0;
                      }
                      
                        
                   if  (  SnR [Place]>= High[i] - Tolerance*Point && SnR[Place] <= High[i] + Tolerance * Point ) // Maybe a problem
                      {
                        SnR[Place] =(SnRCount[Place]*SnR[Place] + High[i]) / (SnRCount[Place] +1);
                        SnR[Place] = NormalizeDouble(SnR[Place],Digits);
                        SnRCount [Place]++; // if the levels matched count that level
                      }
                      
                   
                   else 
                      { // if they did not then make a new space
                      ArrayResize(SnR,ArraySize(SnR) + 1);
                      ArrayResize(SnRCount,ArraySize(SnRCount) +1);
                      SnR [ ArraySize(SnR) -1 ] = High[i];
                      SnRCount [ArraySize(SnRCount) -1] = 1;
                      }
                      
                      
                  }    
            
            
      // This Part Finds Support      
         if (Low[i] < Low [i+1] && Low[i] >Low[i-1])
                  {// if a high has been found
               
                    // Compare to previous prices
                   
                   
                   
                   
                   if (ArraySize(SnR) > 0)
                   {
                     Place = ArrayBsearch(SnR,Low[i]);
                   }
                   
                   else // if arraysize is 0 then open 1 cell
                      {
                      ArrayResize(SnR,ArraySize(SnR) + 1);
                      ArrayResize(SnRCount,ArraySize(SnRCount) +1);
                      SnR [ ArraySize(SnR) -1 ] = 0;
                      SnRCount [ArraySize(SnRCount) -1] = 0;
                      }
                      
                        
                   if  (  SnR [Place]>= Low[i] - Tolerance*Point && SnR[Place] <= Low[i] + Tolerance * Point ) // Maybe a problem
                      {
                        SnR[Place] =(SnRCount[Place]*SnR[Place] + Low[i]) / (SnRCount[Place] +1);
                        SnR[Place] = NormalizeDouble(SnR[Place],Digits);
                        SnRCount [Place]++; // if the levels matched count that level
                      }
                      
                   
                   else 
                      { // if they did not then make a new space
                      ArrayResize(SnR,ArraySize(SnR) + 1);
                      ArrayResize(SnRCount,ArraySize(SnRCount) +1);
                      SnR [ ArraySize(SnR) -1 ] = Low[i];
                      SnRCount [ArraySize(SnRCount) -1] = 1;
                      }
                      
                      
                  }    
            
 
         
      }    
   //  Alert (ArraySize(SnR), "Size ", ArraySize(SnRCount));        
        for (i = 0; i< ArraySize(SnR) ; i++)
         {
         if (SnRCount[i] >5)
          {
          Alert ("Price ", SnR[i], " Was hit ", SnRCount[i], " times"); 
          ObjectCreate (DoubleToStr(SnR[i],5),OBJ_HLINE, 0, Time[0], SnR[i]);
          ObjectSet (DoubleToStr(SnR[i],5),OBJPROP_COLOR,White);      
         // ObjectSet (DoubleToStr(SnR[i],5),OBJPROP_LEVELWIDTH ,SnRCount[i]);
          }
         }              
                    
                   
                  
 
   
   
   
   

   
//----
   return(0);
  }
//+------------------------------------------------------------------+